package com.qiyigu.itc.speech.xunfai;

import android.os.Message;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiyigu.itc.speech.service.BaseServiceImpl;
import com.qiyigu.itc.speech.util.Constant;
import com.qiyigu.itc.speech.util.StringUtils;


/**
 * <p>Author: Leo xu</p>
 * <p>Date: 2017/12/7 11:08</p>
 * <p>Desc: 语音 解析器
 */
public class XunfaiSpeechParserServiceImpl extends BaseServiceImpl {

    protected static XunfaiSpeechParserServiceImpl service = null;

    protected XunfaiSpeechParserServiceImpl() {
        service = this;
    }

    public static synchronized XunfaiSpeechParserServiceImpl getInstance() {
        if (service == null) {
            service = new XunfaiSpeechParserServiceImpl();
        }
        return service;
    }

    public Message parserSpeechStr(String strResult) {
        JSONObject jsonObject = JSONObject.parseObject(strResult);
        if (jsonObject == null) {
            return null;
        }

        Message msg = null;
        String strService = jsonObject.getString("service");
        if (StringUtils.isNotEmpty(strService) && strService.equals("LEDEITC.itc_controller")) {
            JSONArray semanticJsons = jsonObject.getJSONArray("semantic");
            if (semanticJsons != null) {
                msg = parserIntentCmd(semanticJsons.getJSONObject(0));
            }
            msg.obj = jsonObject.getString("text");
        }

        if (msg == null || msg.what == 0) {
            msg = new Message();
            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
            String strText = jsonObject.getString("text");
            msg.obj = strText;
        }
        return msg;
    }

    private Message parserIntentCmd(JSONObject semanticJson) {
        String strIntent = semanticJson.getString("intent");
        if (StringUtils.isEmpty(strIntent)) {
            return null;
        }
        XunfaiIntentEnum xunfaiIntentEnum = XunfaiIntentEnum.getEnum(strIntent);
        JSONArray slotJsons = semanticJson.getJSONArray("slots");
        Message msg = null;
        switch (xunfaiIntentEnum) {
            case RUN_CONTROLLER:
                msg = parserRunController(slotJsons);
                break;
            case SPEED_SLOPE_CONTROLLER:
                msg = parserSpeedSlopeContoller(slotJsons);
                break;
            case SPEED_SLOPE_ADD:
                msg = parserSpeedSlopeAdd(slotJsons);
                break;
            case SPEED_SLOPE_REDUCE:
                msg = parserSpeedSlopeReduce(slotJsons);
                break;
            case OPEN_CLOSE_FAN:
                msg = parserOpenCloseFan(slotJsons);
                break;
            case OPEN_APP:
                msg = parserOpenApp(slotJsons);
                break;
            default:
                break;
        }
        return msg;
    }

    /**
     * 开始 暂停 停止
     *
     * @param slotJsons
     * @return
     */
    private Message parserRunController(JSONArray slotJsons) {
        Message msg = null;
        JSONObject jsonObject = slotJsons.getJSONObject(0);
        if (jsonObject != null) {
            String strName = jsonObject.getString("name");
            if (StringUtils.isNotEmpty(strName) && strName.equals("startStopPause")) {
                String strValue = jsonObject.getString("value");
                if (StringUtils.isNotEmpty(strValue)) {
                    msg = new Message();
                    if (strValue.equals("开始")) {
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_START;
                    } else if (strValue.equals("暂停")) {
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_PAUSE;
                    } else if (strValue.equals("停止")) {
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_STOP;
                    }
                }
            }
        }
        return msg;
    }

    /**
     * 速度 坡度 音量控制
     *
     * @param slotJsons
     * @return
     */
    private Message parserSpeedSlopeContoller(JSONArray slotJsons) {
        Message msg = new Message();

        for (int i = 0; i < slotJsons.size(); i++) {
            JSONObject jsonObject = slotJsons.getJSONObject(i);
            if (jsonObject != null) {
                String strName = jsonObject.getString("name");

                if (StringUtils.isNotEmpty(strName) && strName.equals("speedSlope")) {
                    String strValue = jsonObject.getString("value");
                    if (StringUtils.isNotEmpty(strValue)) {
                        if (strValue.equals("速度")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SPEED;
                        } else if (strValue.equals("坡度")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SLOPE;
                        } else if (strValue.equals("音量")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_VOICE;
                        } else {
                            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
                        }
                    }
                } else if (StringUtils.isNotEmpty(strName) && strName.equals("num")) {
                    String strValue = jsonObject.getString("normValue");
                    if (StringUtils.isNotEmpty(strValue)) {
                        try {
                            Integer intValue = Integer.parseInt(strValue);
                            msg.arg1 = intValue;
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }
        return msg;
    }


    /**
     * 速度 坡度 音量 加
     *
     * @param slotJsons
     * @return
     */
    private Message parserSpeedSlopeAdd(JSONArray slotJsons) {
        Message msg = new Message();
        boolean isAdd = false;
        for (int i = 0; i < slotJsons.size(); i++) {
            JSONObject jsonObject = slotJsons.getJSONObject(i);
            if (jsonObject != null) {
                String strName = jsonObject.getString("name");
                String strValue = jsonObject.getString("value");

                if (StringUtils.isNotEmpty(strName) && strName.equals("add")) {
                    if (StringUtils.isNotEmpty(strValue)) {
                        if (strValue.equals("快") || strValue.equals("升") || strValue.equals("加")) {
                            isAdd = true;
                        }
                    }
                } else if (StringUtils.isNotEmpty(strName) && strName.equals("speedSlope")) {
                    if (StringUtils.isNotEmpty(strValue)) {
                        if (strValue.equals("速度")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SPEED_ADD;
                        } else if (strValue.equals("坡度")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SLOPE_ADD;
                        } else if (strValue.equals("音量")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_VOICE_ADD;
                        } else {
                            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
                        }
                    }
                }
            }
        }
        if (!isAdd) {
            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
        }
        return msg;
    }

    /**
     * 速度 坡度 音量 加
     *
     * @param slotJsons
     * @return
     */
    private Message parserSpeedSlopeReduce(JSONArray slotJsons) {
        Message msg = new Message();
        boolean isReduce = false;
        for (int i = 0; i < slotJsons.size(); i++) {
            JSONObject jsonObject = slotJsons.getJSONObject(i);
            if (jsonObject != null) {
                String strName = jsonObject.getString("name");
                String strValue = jsonObject.getString("value");
                if (StringUtils.isNotEmpty(strName) && strName.equals("reduce")) {
                    if (StringUtils.isNotEmpty(strValue)) {
                        if (strValue.equals("低") || strValue.equals("慢") || strValue.equals("降")
                                || strValue.equals("减")) {
                            isReduce = true;
                        }
                    }
                } else if (StringUtils.isNotEmpty(strName) && strName.equals("speedSlope")) {
                    if (StringUtils.isNotEmpty(strValue)) {
                        if (strValue.equals("速度")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SPEED_REDUCE;
                        } else if (strValue.equals("坡度")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SLOPE_REDUCE;
                        } else if (strValue.equals("音量")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_VOICE_REDUCE;
                        } else {
                            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
                        }
                    }
                }
            }
        }
        if (!isReduce) {
            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
        }
        return msg;
    }

    /**
     * 打开 关闭风扇
     *
     * @param slotJsons
     * @return
     */
    private Message parserOpenCloseFan(JSONArray slotJsons) {
        Message msg = new Message();
        for (int i = 0; i < slotJsons.size(); i++) {
            JSONObject jsonObject = slotJsons.getJSONObject(i);
            if (jsonObject != null) {
                String strName = jsonObject.getString("name");
                String strValue = jsonObject.getString("value");

                if (StringUtils.isNotEmpty(strName) && strName.equals("openClose")) {
                    if (StringUtils.isNotEmpty(strValue)) {
                        if (strValue.equals("打开")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_OPEAN_FAN;
                        } else if (strValue.equals("关闭")) {
                            msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_CLOSE_FAN;
                        } else {
                            msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
                        }
                    }
                }
            }
        }
        return msg;
    }

    /**
     * 打开 App
     *
     * @param slotJsons
     * @return
     */
    private Message parserOpenApp(JSONArray slotJsons) {
        Message msg = new Message();
        for (int i = 0; i < slotJsons.size(); i++) {
            JSONObject jsonObject = slotJsons.getJSONObject(i);
            if (jsonObject != null) {
                String strName = jsonObject.getString("name");
                String strValue = jsonObject.getString("value");
                if (StringUtils.isNotEmpty(strName) && strName.equals("app")) {
                    msg.what = Constant.SpeechParserConstant.SPEECH_APP_HANDLE_OPEN;
                    msg.obj = strValue;
                } else {
                    msg.what = Constant.SpeechParserConstant.SPEECH_UNKNOWN;
                }
            }
        }
        return msg;
    }

}
